/**
 * @author yaoyuan
 * @version 1.0
 * @name zjzcVotingApp.services:http
 * @description 数据请求的AJAX模块
 */

'use strict';

// 定义app的Service模块变量
var services = angular.module('zjzcVotingApp');

//api 请求

//请求数据方法
services.factory('http', ['$http', '$q', function ($http, $q) {

    function ajax(params, options){
     //   options = options || {};
        var def = $q.defer();
        $http(options || {
            //url: options.url || url2,
            url: server+ params.method,
            method:  'get',
            params: params
        }).success(function (data) {
            if (data.retCode == '0000') {
                var res = data.data || {};
                res.retMsg =  data.retMsg;
                def.resolve(res);
            } else {
                def.reject({
                    retMsg: data.retMsg
                });
            }
        }).error(function (data) {
            def.reject({
                retMsg: data.retMsg || '获取失败'
            });
        });

        return def.promise;
    }

    function post(params){
        return ajax(params, {
            url: server+params.method,
            method: 'POST',
            data: params
        });
    }
    return {
        ajax: ajax,
        post: post
    };
}]);

